package com.whitesalt.inputfix;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Objects;

/**
 * @ White_Salt
 * 创建于 2017/10/14
 * powered by: IntelliJ IDEA
 */
public class GUI
{
    private JComboBox type;
    private JTextField command;
    JPanel mainPanel;
    private JButton send;


    GUI()
    {

        send.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String cmd = command.getText();
                if(Objects.requireNonNull(type.getSelectedItem()).toString().equals("命令"))
                {
                    AsynCommand.dispatchCommand(cmd);
                    command.setText("");
                }
                else if(type.getSelectedItem().toString().equals("聊天"))
                {
                    AsynCommand.dispatchCommand("say "+cmd);
                    command.setText("");
                }

            }
        });

        // ENTER键监听
        command.addKeyListener(new KeyAdapter()
        {
            @Override
            public void keyPressed(KeyEvent e)
            {
                if(e.getKeyCode()==KeyEvent.VK_ENTER)
                {
                    String cmd = command.getText();
                    if(Objects.requireNonNull(type.getSelectedItem()).toString().equals("命令"))
                    {
                        AsynCommand.dispatchCommand(cmd);
                        command.setText("");
                    }
                    else if(type.getSelectedItem().toString().equals("聊天"))
                    {
                        AsynCommand.dispatchCommand("say "+cmd);
                        command.setText("");
                    }
                }
            }
        });
    }
}
